<?php
class Adver extends BaseClass {
	private $id;
	private $props;
	private $new_props;

	private $BD;
	private $LOGGER;
	private $Drills;

	//===================================================================================

	public function __construct($id = false) {
		parent::__construct();

		$this->id = false;
		$this->props = array();
		$this->new_props = array();

		$this->DB = Mysql::getInstance();
		$this->LOGGER = Logger::getInstance();
		$this->Drills = Drills::getInstance();

		$this->load($id);
	}

	//===================================================================================

	public function load($id) {
		if (empty($id)) return;

		$this->id = $id;
		$this->props = array();
		$this->new_props = array();
	}

	//===================================================================================

	public function setId($id) {
		if (empty($id)) return;

		$this->id = $id;
	}

	//===================================================================================

	public function save() {
		$this->props = array_merge($this->new_props, $this->props);
		$this->new_props = array();
	}

	//===================================================================================

	public function addProp($prop, $value) {
		if (empty($prop)) return;

		$this->new_props[$prop] = $value;
	}

	//===================================================================================

	public function getProp($prop) {
		if (empty($prop)) return;

		if (isset($this->new_props[$prop]))
			return $this->new_props[$prop];

		if (isset($this->props[$prop]))
			return $this->props[$prop];

		return false;
	}

	//===================================================================================

	public function getProps() {
		return array_merge($this->props, $this->new_props);
	}

	//===================================================================================

	public function addToDatabase() {

		global $G;

		$drills_text = array();
		$insert_items = array();
		$value_items = array();
		foreach ($this->getProps() as $drill_key => $drill_value) {
			if (!isset($G["ad_drills"][$drill_key])) {
				continue;
			}
			$drill_id = $this->Drills->storeDrill($drill_key, $drill_value);

			if ($drill_id === false) {
				return false;
			}

			array_push($drills_text, $drill_value);
			array_push($insert_items, $G["ad_drills"][$drill_key]["param_field_name"]);
			array_push($value_items, $drill_id);
		}

		$query = "
			INSERT INTO
				`ads` (
					`c_date`,
					".implode(",", $insert_items)."
				)
			VALUES (
				".time().",
				".implode(",", $value_items)."
			)";
		if ($this->DB->query($query)->isError()) {
			$this->LOGGER->log("Failed to insert ads in database. Reason: ".$this->DB->getErrors());
			return false;
		}


		$query = "
			INSERT INTO
				`d` (
					`text`
				)
			VALUES (
				\"".implode(",", $drills_text)."\"
			)";
		if ($this->DB->query($query)->isError()) {
			$this->LOGGER->log("Failed to insert drill text in database. Reason: ".$this->DB->getErrors());
			return false;
		}

		return true;
	}

	//===================================================================================
}

?>
